package com.expertria.glex.xhr;
 
 
import com.expertria.glex.event.Events.EventDispatcherHelper;
import com.expertria.glex.event.Events.IEventDispatcher;
import com.expertria.glex.event.Events.IEventListener;
import com.google.gwt.core.client.JavaScriptObject;
 
public   class AsyncToken implements IEventDispatcher , IJSONResponder
		 
{
	public static final class Events
	{
		public static final String success = "success", failure = "failure";
	}
	EventDispatcherHelper dispatcher = new EventDispatcherHelper();

	@Override
	public void addEventListener(String event, IEventListener e) {
		dispatcher.addEventListener(event, e);

	}

	@Override
	public void dispatch(String eventName, Object object) {
		dispatcher.dispatch(eventName, object);
	}

	/*
	@Override
	public void onFailure(String reason, SimpleDelegate delegate) {
		// DEFAULT
		this.dispatch(Event.FAILURE, delegate);
	}*/

	@Override
	public void dispose() {

		response = null;
		request = null;
		dispatcher.dispose();
	}
	
	private Object response;
	private Object request;
	
	public void onRequestComplete( JavaScriptObject json , JSONRequest rq)
	{
		this.response = json;
		this.request  = rq;
		dispatch(Events.success, this);
	}
	
	public Object getRequest() {
		return request;
	}
	public Object getResponse() {
		return response;
	}
	
}
